import { page } from '@/common/utils'
import en from '@/locale/en.json'
import fo from '@/locale/fo.json'
import jaJP from '@/locale/ja-JP.json'
import ko from '@/locale/ko.json'
import zhHans from '@/locale/zh-Hans.json'
import zhHant from '@/locale/zh-Hant.json'
import pagesJson from '@/pages.json'

const locale : Record<string, Record<string, string>> = {
	en,
	fo,
	ko,
	'ja-JP': jaJP,
	'zh-Hans': zhHans,
	'zh-Hant': zhHant
}
const pageTitle = (function () {
	let arr : Record<string, string> = {};
	(pagesJson as any).pages.forEach((item : any) => {
		if (item.style?.navigationBarTitleText) {
			arr[item.path] = item.style?.navigationBarTitleText.substring(1).substring(0, -1)
		}
	});
	(pagesJson as any).subPackages?.forEach((item : any) => {
		item.pages?.forEach((i : any) => {
			if (i.style?.navigationBarTitleText) {
				arr[`${item.root}/${i.path}`] = i.style?.navigationBarTitleText.substring(1).slice(0, -1)
			}
		})
	})
	return arr
})()
export default {
	onReady() {
		// const appLocale = uni.getLocale()
		const appLocale = 'zh-Hant'
		const i18 = locale[appLocale]
		const title = i18[pageTitle[`${page().substring(1)}`]]
		if (title) {
			uni.setNavigationBarTitle({
				title
			})
		}
	}
}